import axios from 'axios'

const request = axios.create({
  baseURL: '/',
  headers: {
    'Content-Type': 'application/json'
  },
  withCredentials: false,
  timeout: 6000
})

// 异常拦截处理方法
const handleError = (error: object) => {
  return Promise.reject(error)
}

// 请求拦截器
request.interceptors.request.use((config) => {
  return config
}, handleError)

// 响应拦截器
request.interceptors.response.use((response) => {
  if (response.data.code === 200) {
    return Promise.resolve(response.data)
  } else {
    return Promise.reject(response.data)
  }
}, handleError)

export default request
